package com.action;

import com.dao.TUserDAO;
import com.model.TUser;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Cart;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

public class UserAction extends ActionSupport {
    private int userId;
    private String userName;
    private String userPw;
    private String userRealname;
    private String userAddress;
    private String userSex;
    private String userTel;
    private String userEmail;
    private String userQq;

    private String newUserPw;


    private String message;
    private String path;

    private TUserDAO userDAO;


    public String userReg() {
        TUser user = new TUser();
        user.setUserName(userName);
        user.setUserPw(userPw);
        user.setUserAddress(userAddress);
        user.setUserTel(userTel);
        user.setUserRealname(userRealname);
        user.setUserEmail(userEmail);
        user.setUserSex(userSex);
        user.setUserQq(userQq);
        user.setUserDel("no");
        userDAO.save(user);
        Map session = ServletActionContext.getContext().getSession();
        session.put("user", user);
        return "successAdd";
    }

    public String userEdit() {
        TUser user = userDAO.findById(userId);
        user.setUserName(userName);
        user.setUserPw(userPw);
        user.setUserAddress(userAddress);
        user.setUserTel(userTel);
        user.setUserRealname(userRealname);
        user.setUserEmail(userEmail);
        user.setUserSex(userSex);
        user.setUserQq(userQq);
        user.setUserDel("no");
        userDAO.attachDirty(user);
        Map session = ServletActionContext.getContext().getSession();
        session.put("user", user);
        return "successAdd";
    }


    public String userLogin() {
        String sql = "from TUser where userName=? and userPw=?";
        Object[] con = {userName, userPw};
        List userList = userDAO.getHibernateTemplate().find(sql, con);
        if (userList.size() == 0) {
            this.setMessage("用户名或密码错误！");
            this.setPath("shop/default.jsp");
        } else {
            Map session = ServletActionContext.getContext().getSession();
            TUser user = (TUser) userList.get(0);
            session.put("user", user);

            Cart cart = new Cart();
            session.put("cart", cart);

            this.setMessage("登录成功！");
            this.setPath("shop/default.jsp");
        }
        return "succeed";
    }


    public String userLogout() {
        Map session = ServletActionContext.getContext().getSession();
        session.remove("user");
        return ActionSupport.SUCCESS;
    }


    public String userDel() {
        TUser user = userDAO.findById(userId);
        user.setUserDel("yes");
        userDAO.attachDirty(user);
        this.setMessage("删除成功！");
        this.setPath("userMana.action");
        return "succeed";
    }


    public String userXinxi() {
        TUser user = userDAO.findById(userId);
        Map request = (Map) ServletActionContext.getContext().get("request");
        request.put("user", user);
        return ActionSupport.SUCCESS;
    }


    public String userMana() {
        List userList = userDAO.findAll();
        Map request = (Map) ServletActionContext.getContext().get("request");
        request.put("userList", userList);
        return ActionSupport.SUCCESS;
    }


    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserQq() {
        return userQq;
    }

    public void setUserQq(String userQq) {
        this.userQq = userQq;
    }

    public String getUserRealname() {
        return userRealname;
    }

    public void setUserRealname(String userRealname) {
        this.userRealname = userRealname;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public String getUserTel() {
        return userTel;
    }

    public void setUserTel(String userTel) {
        this.userTel = userTel;
    }

    public int getUserId() {
        return userId;
    }


    public void setUserId(int userId) {
        this.userId = userId;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public TUserDAO getUserDAO() {
        return userDAO;
    }


    public String getMessage() {
        return message;
    }


    public String getNewUserPw() {
        return newUserPw;
    }

    public void setNewUserPw(String newUserPw) {
        this.newUserPw = newUserPw;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public String getPath() {
        return path;
    }


    public void setPath(String path) {
        this.path = path;
    }


    public void setUserDAO(TUserDAO userDAO) {
        this.userDAO = userDAO;
    }


    public String getUserPw() {
        return userPw;
    }

    public void setUserPw(String userPw) {
        this.userPw = userPw;
    }


}
